#include<iostream>

using namespace std;
const int N=5e4+10;
int f[N*3];

int find(int x)
{
	return f[x]==x?x:f[x]=find(f[x]);
}

void un(int x,int y)
{
	x=find(x);
	y=find(y);

	if(x==y) return ;
	if(x>y) swap(x,y);
	f[y]=x;
}

bool issame(int x,int y)
{
	return find(x)==find(y);
}

bool authenticity(int n)
{
	int k,x,y;cin>>k>>x>>y;
	if(x>n||y>n) return false;

	if(k==1)
	{
		if(issame(x+n,y)||issame(x+2*n,y))
			return false;
		un(x,y);
		un(x+n,y+n);
		un(x+2*n,y+2*n);
	}
	else
	{
		if(issame(x,y)||issame(x+2*n,y))
			return false;
		un(x+n,y);
		un(x+2*n,y+n);
		un(x,y+2*n);
	}
	return true;
}
int main()
{
	int n,k;cin>>n>>k;
	for(int i=1;i<=3*n;i++)
	{
		f[i]=i;
	}

	int ret=0;
	for(int i=1;i<=k;i++)
	{
		if(!authenticity(n))
		{
			ret++;
		}
	}

	cout<<ret<<endl;
}